﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using EventFlowLibrary;
using EventFlowLibrary.GameStructure;

namespace PerformanceUtility.GameDebugTools
{
    /// <summary>
    /// Class for enqueuing events as they happen through the game.
    /// </summary>
    public class GameDebugger : DrawableGameComponent
    {
        /// <summary>
        /// Our reference to the debug manager.
        /// </summary>
        private DebugManager debugManager;

        /// <summary>
        /// Maximum number of events we track to display.
        /// </summary>
        private World world;

        public GameDebugger(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
#if TRACE
            // Get debug manager from game service.
            debugManager = Game.Services.GetService(typeof(DebugManager)) as DebugManager;

            if (debugManager == null)
                throw new InvalidOperationException("DebugManager is not registered.");

            // Register 'ed' command if debug command is registered as a service.
            IDebugCommandHost host = Game.Services.GetService(typeof(IDebugCommandHost))
                                                                as IDebugCommandHost;

            if (host != null)
            {
                host.RegisterCommand("ge", "Game Event Debugger", this.CommandExecute);
                Visible = false;
            }

            base.Initialize();
#endif
        }

        public void SetWorld(World _world)
        {
            world = _world;
        }

        /// <summary>
        /// FPS command implementation.
        /// </summary>
        private void CommandExecute(IDebugCommandHost host, string command, IList<string> arguments)
        {
            // Show all states and values
            if (arguments.Count == 0)
            {
                foreach (KeyValuePair<string, string> k in GlobalVariables.loadVariables.gameState)
                {
                    host.Echo(k.Key + " : " + k.Value);
                }
            }

            // Echo the selected game state and value
            else if (arguments.Count == 1)
            {
                if (GlobalVariables.loadVariables.gameState.ContainsKey(arguments[0]))
                {
                    host.Echo(arguments[0] + " : " + GlobalVariables.loadVariables.gameState[arguments[0]]);
                }
                else
                {
                    host.EchoError(arguments[0] + " is not a valid game state.");
                }
            }

            // Select only the first two args. This can be modified later for any number of variables.
            else if (arguments.Count == 2)
            {
                if (GlobalVariables.loadVariables.gameState.ContainsKey(arguments[0]))
                {
                    GlobalVariables.loadVariables.gameState[arguments[0]] = arguments[1];
                    world.eventManager.RefreshGameStates();
                    host.Echo("New GameStateValue: " + arguments[0] + " : " + GlobalVariables.loadVariables.gameState[arguments[0]]);
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}
